<?php
declare(strict_types=1);

namespace app\model;

use think\Model;

class PlatformOperationLog extends Model
{
    protected $autoWriteTimestamp = true;

    // 操作类型常量
    const TYPE_CREATE = 1; // 新增
    const TYPE_UPDATE = 2; // 修改
    const TYPE_DELETE = 3; // 删除

    // 类型文字说明
    public static $typeTexts = [
        self::TYPE_CREATE => '新增',
        self::TYPE_UPDATE => '修改',
        self::TYPE_DELETE => '删除'
    ];

    // 关联管理员
    public function admin()
    {
        return $this->belongsTo(Admin::class);
    }

    // 获取操作类型文字
    public function getOperationTypeTextAttr()
    {
        return self::$typeTexts[$this->operation_type] ?? '未知';
    }

    // 记录操作日志
    public static function record(int $adminId, int $operationType, string $content): bool
    {
        return self::create([
            'admin_id' => $adminId,
            'operation_type' => $operationType,
            'operation_content' => $content,
        ]) ? true : false;
    }

    // 批量记录操作日志
    public static function recordBatch(array $logs): bool
    {
        return self::insertAll($logs) ? true : false;
    }

    // 获取指定时间范围内的操作日志
    public static function getLogsByDateRange(string $startDate, string $endDate)
    {
        return self::whereBetweenTime('create_time', $startDate, $endDate)
            ->order('id', 'desc')
            ->select();
    }
} 